<?php
class supplierController extends baseController {

    public function show() {
        $ids = Db::get('demand_items')->select('supplier_id')->group('supplier_id')->getCols();
        
        $where = array();
        $count = Supplier::count($where);
        $pager = PagerHelper::split($count);
        $list  = Supplier::getList($where, $pager['r_offset'], $pager['p_size']);
        
        $this->assign('ids',  $ids);
        $this->assign('suppliers',  $list);
        $this->display();
    }
    
    public function price() {
        $where  =   "1";
        if(fetch_a_string('title')) {
            $where  .=  ' AND '.WareHelper::makupFuzzyKey(fetch_a_string('title'));
        }
        $count = WareHelper::count($where);
        $pager = PagerHelper::split($count);

        $slist  = WareHelper::getListWithSuppliers($where, true, 'sno ASC');

        $this->assign('wares',$slist);
        $this->display();
    }

    protected function quote_list(){
    $where  = array();

            if(fetch_a_int('supplier_id')){
                    $where['supplier_id'] = fetch_a_int('supplier_id');
            }
            if(fetch_a_string('keyw')){
                    $where['keyw'] = fetch_a_string('keyw');
            }

            $count = SupplierQuoteHelper::fuzzyCount($where);
            $pager = PagerHelper::split($count);
            $list  = SupplierQuoteHelper::getFuzzyList($where, $pager['r_offset'], $pager['p_size']);

            $this->assign('offers',	$list);
            $this->assign('suppliers', Supplier::getList());
            $this->display('supplier-quote_list');
    }

    public function quote() {
            $id = fetch_a_int('id'); //特定供应商ID

            if(!$id){
                    //查看全部供应商报价
                    return $this->quote_list();
            }

            if(!empty($_POST['cost'])) {
                    foreach($_POST['cost'] as $sid=>$c)
                    {
                            if(!floatval($c)){
                                    Db::get('SupplierQuote')->delete()->where(array(
                                            'supplier_id'=>$id, 
                                            'ware_id'	=>$sid, 
                                    ))->exec();
                            }
                            else{
                                    Db::get('SupplierQuote')->replace(array(
                                            'supplier_id'=>$id, 
                                            'ware_id'	=>$sid, 
                                            'quote'		=>$c, 
                                            'modtime'	=>time()))->exec();
                            }
                    }
                    Show::note('保存成功');
            }

            $where	= array();
    if(fetch_a_string('keyw')) {
        $where[] =  WareHelper::makupFuzzyKey(fetch_a_string('keyw'));
    }

    $list  = WareHelper::getList(join(' AND ', $where), true, 'sno ASC');

            $this->assign('stuffs',		$list);
            $this->assign('suppliers',	Supplier::getList());
            $this->display();
    }

    /**
     * 取供应商报价
     */
    public function offer()
    {
            $sid = fetch_a_int('sid');
            if(!$sid){
                    Show::warn('请选择供应商');
            }
            $list = SupplierQuoteHelper::getFuzzyList(array('supplier_id'=>$sid));
            $func = function(&$row){
                    if($row){
                            $row['quote'] = fnum($row['quote']);
                    }
            };
            array_walk($list, $func);
            Show::ajaxBand(array('list'=>$list));
            Show::note('ok');
    }

    public function modify(){
            $id = fetch_a_int('id');
            if(!$id){
                    Show::warn('请选择供应商');
            }
            $supplier = Supplier::get($id);
            if(!$supplier){
                    Show::warn('无此供应商');
            }

            if(is_post()) {
                    Supplier::modify($id, array(
                                    'name' => fetch_post('name'),
                                    'phone' => fetch_post('phone'),
                                    'linkman' => fetch_post('linkman'),
                                    'address' => fetch_post('address'),
                                    'remark' => fetch_post('remark'),
                                    'full_name' => fetch_post('full_name'),
                            ));
                    Show::note('保存成功');
            }

            $this->assign('supplier', $supplier);
            $this->display('supplier-add');
    }

    public function add(){
            if(is_post()) {
                    Supplier::add(array(
                                    'name' => fetch_post('name'),
                                    'phone' => fetch_post('phone'),
                                    'linkman' => fetch_post('linkman'),
                                    'address' => fetch_post('address'),
                                    'remark' => fetch_post('remark'),
                                    'full_name' => fetch_post('full_name'),
                            ));
                    Show::note('添加成功');
            }

            $this->display();
    }
    //增加删除
    public function delete(){
            $id = fetch_a_int('id');
            if($id) {
                //检查供应商是否已经被使用
                $ids = Db::get('demand_items')->select('supplier_id')->group('supplier_id')->getCols();
                if(in_array($id,$ids))
                {
                    Show::note('不能删除已经使用的供应商数据',url('supplier'));
                    
                }
                else
                {
                    $isOK = Supplier::delete($id);
                    if($isOK)
                    {
                        //删除报价 
                        Db::get('SupplierQuote')->delete()->where(array(
                                            'supplier_id'=>$id
                                    ))->exec();
                    }
                    Show::note('删除成功',url('supplier'));                
                }
            }
    }
	
}
?>
